//
//  KZAnswerClassFillView.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2019/1/9.
//  Copyright © 2019 zhukai. All rights reserved.
//

#import "KZAnswerClassFillingView.h"

@implementation KZAnswerClassFillingView
{
    UIScrollView *_inputScroll;         // 输入区域
    NSMutableArray *_inputContents;     // 输入内容数组
    
    KZShadowButton *_submitButton;      // 提交按钮
}

- (void)setupUI {
    
    _submitButton = [[KZShadowButton alloc] init:^(UILabel *label) {
        label.text = @"提交";
    }];
    [self addSubview:_submitButton];
    [_submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(W(20));
        make.right.mas_equalTo(W(-20));
        make.bottom.mas_equalTo(H(46));
        make.height.mas_equalTo(H(36));
    }];
    
    _inputScroll = [UIScrollView new];
    [self addSubview:_inputScroll];
    [_inputScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        // 输入区域的高度会根据提交按钮变动
        make.bottom.equalTo(self->_submitButton.mas_top);
    }];
    
    // 初始化输入区
    _inputContents = [NSMutableArray new];
    UIView *last = nil;
    for (int i = 0; i < _model.answerCount; i ++) {
        
        // 初始化输入区
        [_inputContents addObject:@""];
        
        UIView *view = [UIView new];
        [_inputScroll addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.width.mas_equalTo(self);
            make.height.mas_equalTo(H(30));
            if (last) {
                make.top.equalTo(last.mas_bottom).offset(H(10));
            } else {
                make.top.mas_equalTo(H(10));
            }
        }];
        last = view;
        
        UILabel *numberLabel = [[UILabel alloc] init];
        numberLabel.text = [NSString stringWithFormat:@"%i.", i];
        numberLabel.font = FONT(14);
        numberLabel.textAlignment = NSTextAlignmentLeft;
        numberLabel.textColor = [UIColor blackColor];
        [view addSubview:numberLabel];
        [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(W(10));
            make.centerY.equalTo(view);
            make.width.mas_equalTo(W(30));
        }];
        
        UITextField *tf = [UITextField new];
        tf.delegate = self;
        tf.tag = i + 10;
        tf.borderStyle = UITextBorderStyleLine;
        [view addSubview:tf];
        [tf mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(numberLabel.mas_right);
            make.top.height.equalTo(view);
            make.width.equalTo(view).offset(W(-100));
        }];
        
        UIImageView *recordView = [UIImageView new];
        recordView.tag = i + 100;
        recordView.backgroundColor = kBlueColor;
        recordView.userInteractionEnabled = true;
        [view addSubview:recordView];
        [recordView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(W(25));
            make.centerY.equalTo(view);
            make.left.equalTo(tf.mas_right).offset(W(10));
        }];
        UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(event:)];
        [recordView addGestureRecognizer:press];
        
    }
    
    
    [last mas_makeConstraints:^(MASConstraintMaker *make) {
        // 决定self的高度
        make.bottom.mas_equalTo(-H(20));
    }];
}

- (void)setModel:(KZExamItemModel *)model {
    
    _model = model;
    [self setupUI];
}
- (void)event:(UILongPressGestureRecognizer *)press {
    
    NSInteger index = press.view.tag - 100;
    UITextField *tf = [self viewWithTag:index + 10];
    
    KZRecordEvaluationTool *tool = [KZRecordEvaluationTool new];

    switch (press.state) {
        case UIGestureRecognizerStateBegan:
        {
            [tool startRecordWithWordsCallback:^(NSString *sentence, NSArray *words) {
                tf.text = sentence;
                [self->_inputContents replaceObjectAtIndex:index withObject:sentence];
                
                NSLog(@"%@", _inputContents);
                [self judgeSubmitButtonNeedShow];
            }];
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            [tool endRecord];
        }
            break;
        default:
            break;
    }
}


#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    NSInteger index = textField.tag - 10;
    [_inputContents replaceObjectAtIndex:index withObject:textField.text];

    NSLog(@"%@", _inputContents);
    [self judgeSubmitButtonNeedShow];
}

#pragma mark - 辅助方法
// 判断提交按钮是否显示
- (void)judgeSubmitButtonNeedShow {
    
    __block isNeed = true;
    [_inputContents enumerateObjectsUsingBlock:^(NSString *text, NSUInteger idx, BOOL * _Nonnull stop) {
        if (text.length == 0) {
            isNeed = false;
            *stop = true;
        }
    }];
    
    [UIView animateWithDuration:0.25 animations:^{
        if (isNeed) {
            [_submitButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(H(-10));
            }];
        } else {
            [_submitButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(H(46));
            }];
        }
        [self layoutIfNeeded];
    }];
    
}

@end
